/*
 * This program is free software: you can redistribute it and/or modify it under
 * the terms of the GNU General Public License as published by the Free Software
 * Foundation, either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
 * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
 * details.
 *
 * You should have received a copy of the GNU General Public License along with
 * this program. If not, see <http://www.gnu.org/licenses/>.
 */
package org.merak.core.model;

import java.io.Serializable;

import org.merak.core.annotation.Skip;

public abstract class Persistent<T extends Serializable & Comparable<T>>
              extends Identified<T>
{

	//~ Constructors /////////////////////////////////////////////////////////////
	//****************************************************************************
	public Persistent() {
		super(null);
	}
	//****************************************************************************
	public Persistent(T id) {
		super(id);
	}

	//~ Methods //////////////////////////////////////////////////////////////////
	//****************************************************************************
	/** The instance is currently associated with a persistence context. It has a
	 *  persistent identity (primary key value) and can have a corresponding row
	 *  in the database */
	@Skip
	public boolean isPersisted() {
		return !this.hasId(null);
	}
	//****************************************************************************
	/** The instance is not associated with any persistence context. It has no
	 *  persistent identity or primary key value.  */
	@Skip
	public boolean isTransient() {
		return this.hasId(null);
	}
	//****************************************************************************
	/** If transient, the instance must be saved to the database. If persistent,
	 *  it must be just updated in the database. */
	public abstract void commit() throws Exception;

	//****************************************************************************
	/** If persistent, the instance must be removed from the database. If transient,
	 *  it is a good practice to throw an IOException. */
	public abstract void delete() throws Exception;

	//****************************************************************************

}
